package com.zbkj.crmeb.merchant.api.controller;

import com.common.CommonPage;
import com.crmeb.core.common.CommonResult;
import com.common.PageParamRequest;
import com.google.common.collect.Lists;
import com.zbkj.crmeb.front.request.UserAddressDelRequest;
import com.zbkj.crmeb.front.request.UserAddressRequest;
import com.zbkj.crmeb.user.model.UserAddress;
import com.zbkj.crmeb.user.service.UserAddressService;
import com.zbkj.crmeb.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * 用户地址 前端控制器
 */
@Slf4j
@RestController
@RequestMapping("api/front/mer/address")
@Api(tags = "用户 -- 地址")
public class FontUserAddressController extends ApiBaseController {

    @Autowired
    private UserAddressService userAddressService;

    @Autowired
    private UserService userService;


    /**
     * 分页显示用户地址
     * @author Mr.Zhang
     * @since 2020-04-28
     */
    @ApiOperation(value = "列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<UserAddress>>  getList(Integer[] areaType){
        UserAddress userAddress = new UserAddress();
        userAddress.setUid(userService.getUserId());
        userAddress.setIsDel(false);
        if(ArrayUtils.isNotEmpty(areaType)){
            userAddress.setAreaTypes(Lists.newArrayList(areaType));
        }
        //默认查100个，不要分页了
        PageParamRequest pageParamRequest = new PageParamRequest();
        pageParamRequest.setPage(1);
        pageParamRequest.setLimit(100);
        CommonPage<UserAddress> userAddressCommonPage = CommonPage.restPage(userAddressService.getListByUserAddress(userAddress,pageParamRequest));
        return CommonResult.success(userAddressCommonPage);
    }

    /**
     * 根据区域获取自提地址
     * @param areaType
     * @return
     */
    @ApiOperation(value = "根据区域获取自提地址")
    @GetMapping(value = "/getTakeAddress")
    public CommonResult<Object>  getTakeAddress(@ApiParam(value = "区域类型", required = true) @RequestParam List<Integer> areaType, HttpServletRequest request){
        return CommonResult.success(userAddressService.getTakeAddress(areaType,getMerId(request)));
    }


    /**
     * 新增用户地址
     * @param request 新增参数
     * @author Mr.Zhang
     * @since 2020-04-28
     */
    @ApiOperation(value = "保存")
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public CommonResult<UserAddress> save(@RequestBody @Validated UserAddressRequest request){
        return CommonResult.success(userAddressService.create(request));
    }

    /**
     * 删除用户地址
     * @param request UserAddressDelRequest 参数
     * @author Mr.Zhang
     * @since 2020-04-28
     */
    @ApiOperation(value = "删除")
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public CommonResult<String> delete(@RequestBody UserAddressDelRequest request){
        if(userAddressService.delete(request.getId())){
            return CommonResult.success();
        }else{
            return CommonResult.failed();
        }
    }

    /**
     * 详情
     * @param id Integer
     * @author Mr.Zhang
     * @since 2020-04-28
     */
    @ApiOperation(value = "获取单个地址")
    @RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
    public CommonResult<UserAddress> info(@PathVariable("id") Integer id){
        UserAddress userAddress = userAddressService.getById(id);
        return CommonResult.success(userAddress);
    }

    /**
     * 获取默认地址
     * @author Mr.Zhang
     * @since 2020-04-28
     */
    @ApiOperation(value = "获取默认地址")
    @RequestMapping(value = "/default", method = RequestMethod.GET)
    public CommonResult<UserAddress> getDefault(){
        return CommonResult.success(userAddressService.getDefault());

    }

    /**
     * 设置默认地址
     * @param request UserAddressDelRequest 参数
     * @author Mr.Zhang
     * @since 2020-04-28
     */
    @ApiOperation(value = "设置默认地址")
    @RequestMapping(value = "/default/set", method = RequestMethod.POST)
    public CommonResult<UserAddress> def(@RequestBody UserAddressDelRequest request){
        if(userAddressService.def(request.getId())){
            return CommonResult.success();
        }else{
            return CommonResult.failed();
        }
    }

    /**
     * 支付人身份证号校验
     */
    @ApiOperation(value = "支付人身份证号校验")
    @RequestMapping(value = "/verify/idCar", method = RequestMethod.POST)
    public CommonResult<String> verifyIdCar(@RequestBody UserAddressRequest request){
        return userAddressService.verifyIdCard(request);
    }
}



